package top.lisicheng.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

import static top.lisicheng.common.constant.RabbitMqConstant.*;

/**
 * 配置交换机和队列之间的绑定关系
 *
 * @author Lisicheng
 */
@Configuration
public class RabbitMqConfiguration {

    @Bean
    public Exchange fanoutExchange() {
        return new FanoutExchange(FANOUT_EXCHANGE);
    }

    @Bean
    public Queue wxQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", "dead-exchange");
        args.put("x-dead-letter-routing-key", "dead-exchange-router-key");
        return new Queue(WX_QUEUE, true, false, false, args);
    }

    @Bean
    public Queue qqQueue() {
        return new Queue(QQ_QUEUE, true);
    }

    @Bean
    public Queue ddQueue() {
        return new Queue(DD_QUEUE, true);
    }

    @Bean
    public Binding qqBinding() {
        return BindingBuilder.bind(qqQueue()).to(fanoutExchange()).with("").noargs();
    }

    @Bean
    public Binding wxBinding() {
        return BindingBuilder.bind(wxQueue()).to(fanoutExchange()).with("").noargs();
    }

    @Bean
    public Binding ddBinding() {
        return BindingBuilder.bind(ddQueue()).to(fanoutExchange()).with("").noargs();
    }

    // @Bean
    // public RabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
    //     SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory = new SimpleRabbitListenerContainerFactory();
    //     simpleRabbitListenerContainerFactory.setConnectionFactory(connectionFactory);
    //     simpleRabbitListenerContainerFactory.setMessageConverter(new Jackson2JsonMessageConverter());
    //     return simpleRabbitListenerContainerFactory;
    // }
}
